package it.polimi.meteocal.web.beans;

import it.polimi.meteocal.ejb.notificationmanagement.NotificationDTO;
import it.polimi.meteocal.ejb.eventmanagement.EventMgr;
import it.polimi.meteocal.ejb.eventmanagement.Event;
import it.polimi.meteocal.ejb.notificationmanagement.NotificationMgr;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import net.aksingh.java.api.owm.CurrentWeatherData;
import net.aksingh.java.api.owm.DailyForecastData;
import net.aksingh.java.api.owm.DailyForecastData.Forecast;
import net.aksingh.java.api.owm.OpenWeatherMap;

import org.json.JSONException;


@ManagedBean(name="meteoBean")
@RequestScoped
public class MeteoBean {
	//String jsonUrl = "http://api.openweathermap.org/data/2.5/weather?q=Milan,it";

	@EJB
	private EventMgr eventMgr;
	
	@EJB
	private NotificationMgr notificationMgr;
	
	private List<?> events;
	    
	private List<Event> list;
	 
	private static List<Forecast> globalForecast;
	
    public static void meteo(String place)
            throws IOException, MalformedURLException, JSONException {

        // declaring object of "OpenWeatherMap" class
        OpenWeatherMap owm = new OpenWeatherMap("ece6dc34f3d35f94678c0fa7886b162b");

        // getting current weather data for the "London" city
        //CurrentWeatherData cwd = owm.currentWeatherByCityName("London");
        
        if (place.equals("") || place.equals(null)){
        	place = "Milan";
        	System.out.println("Error retrieve place, Milan will be used [code1]");
        }else{
        	System.out.println("Place selected for weather: "+place);
        }
        
        
        DailyForecastData forecast = owm.dailyForecastByCityName(place, (byte) 0);
        
        if (forecast.equals(null)){
            forecast = owm.dailyForecastByCityName("Milan", (byte) 0);
        	System.out.println("Error retrieve place, Milan will be used [code2]");
        }
        
        //printing city name from the retrieved data
        //System.out.println("City: " + cwd.getCityName());
        
        
        // printing the max./min. temperature
        //System.out.println("Temperature: " + cwd.getMainData_Object().getMaxTemperature()
        //                    + "/" + cwd.getMainData_Object().getMinTemperature() + "\'F");
    
        globalForecast = forecast.getForecast_List();
        
        //forecast2.get(0).getTemperature_Object().getDayTemperature();
        //System.out.println(forecast.getForecast_List().get(0).getTemperature_Object().getDayTemperature());
        //System.out.println(forecast.getForecast_List_Count());
        //System.out.println(forecast.getForecast_List().get(0).getDateTime());
        //System.out.println(forecast.getForecast_List().get(1).getDateTime());
        //System.out.println(forecast.getForecast_List().get(2).getDateTime());
        //System.out.println(forecast.getForecast_List().get(1).getWeather_List().get(0).getWeatherName());
    
    
    }
    
    public String getWeather(int d,String place) throws MalformedURLException, IOException, JSONException{
    	meteo(place);
    	if(d>=0){
    		return globalForecast.get(d).getWeather_List().get(0).getWeatherName();
    	}else{
    		return "N/A";
    	}
    }
    
    public float getMaxTemp(int d,String place) throws MalformedURLException, IOException, JSONException{
    	//meteo(place);
    	if(d>=0){
    		//System.out.println("magic "+d+" maxT "+globalForecast.get(d).getTemperature_Object().getMaximumTemperature());
    		return globalForecast.get(d).getTemperature_Object().getMaximumTemperature();
    		
    	}else{
    		return 0;
    	}
    }
    
    public float getMinTemp(int d,String place) throws MalformedURLException, IOException, JSONException{
    	//meteo(place);
    	if(d>=0){
    		return globalForecast.get(d).getTemperature_Object().getMinimumTemperature();
    	}else{
    		return 0;
    	}
    }
    
    public void checkTomorrowWeather() throws MalformedURLException, IOException, JSONException{
    	
    	//if bad weather for tomorrow
    	if(isBadWeather()){
    	
	    	events = eventMgr.myTomorrowEvents();
	    	list = new ArrayList<Event>();
	    	
	    	for(Object elem: events){
	    		list.add((Event) elem);
	    	}
	    	
	    	NotificationDTO notification = new NotificationDTO();
	    	
	    	for(int i=0; i<list.size(); i++){
	    		if(list.get(i).getInsideOutside().equals("o")){
	    			notification.setText("Bad weather expected for tomorrow outside event "+list.get(i).getName());
	    			notification.setType("1");
	    			notificationMgr.save(notification);
	    		}
	    	}
    	}
    	 
    }
    
    public boolean isBadWeather() throws MalformedURLException, IOException, JSONException{
        
    	OpenWeatherMap owm = new OpenWeatherMap("ece6dc34f3d35f94678c0fa7886b162b");
        DailyForecastData forecast = owm.dailyForecastByCityName("Milan", (byte) 0);
        String tomorrowWeather = new String();
        tomorrowWeather = forecast.getForecast_List().get(1).getWeather_List().get(0).getWeatherName();
        if(tomorrowWeather.equals("Rain") || tomorrowWeather.equals("Snow")){
        	//if bad weather for tomorrow
        	return true;
        }
		return false;
	}

}
